package fr.alesia.deepstack.bean.jdo;

import java.util.Date;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import fr.alesia.deepstack.bean.Tournament;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TournamentJdo implements Tournament {

	@PrimaryKey
	@Persistent //(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private String id;
	
	@Persistent
	private Date date;
	
	@NotPersistent
	private boolean running;
	
	public TournamentJdo(Date date) {

		this.date = date;
		this.running = false;	
	}

	public String getId() {
		return id;
	}
	
	public Date getDate() {
		return date;
	}

	public boolean isRunning() {
		return running;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public int compare(Tournament t1, Tournament t2) {
		
		if(t1 == null){
			
			return (t2 == null ? 0 : -1);
		}
		else if(t2 == null){
			
			return (t1 == null ? 0 : 1);
		}
		else{
			if(t1.equals(t2)){
				return 0;
			}
			else{
				if(t1.getDate() == null){
					return(t2.getDate() == null ? 0 : -1);
				}
				return t1.getDate().compareTo(t2.getDate());
			}
		}
	}

	public int compareTo(Tournament other) {

		return this.compare(this, other);
	}

	@Override
	public String toString() {
		
		return id + " [" + date + "]"; 
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TournamentJdo other = (TournamentJdo) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
